package com.zh.web;

import com.zh.po.User;
import com.zh.service.UserService;
import com.zh.web.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * @description: 查询所有用户
     * @return: com.zh.web.vo.ResponseVO<java.util.List<com.zh.po.User>>
     */
    @GetMapping
    public ResponseVO<List<User>> getAll(){
        List<User> users = userService.getAll();
        return new ResponseVO<>(users,"查询成功",2000);
    }

    /**
     * @description: 用户登录
     * @param: loginUser 登录用户
     * @return: com.zh.web.vo.ResponseVO<com.zh.po.User>
     */
    @PostMapping
    public ResponseVO<User> login(@RequestBody User loginUser){
        User user = userService.getUserByLoginName(loginUser.getUserLoginname());
        if (user == null){
            return new ResponseVO<>(null,"登录失败，用户名或密码错误",4000);
        } else if (user.getUserPassword().equals(loginUser.getUserPassword())){
            return new ResponseVO<>(user,"登录成功",2000);
        }
        return new ResponseVO<>(null,"登录失败，用户名或密码错误",4000);
    }

    /**
     * @description: 根据用户id查询用户
     * @param: userId 用户Id
     * @return: com.zh.web.vo.ResponseVO<com.zh.po.User>
     * @author ふわふわ
     * @date: 2021/5/11 9:48
     */
    @GetMapping("/{userId}")
    public ResponseVO<User> getUser(@PathVariable Integer userId){
        User userByUserId = userService.getUserByUserId(userId);
        if (userByUserId==null){
            return new ResponseVO<>(null,"未查询到用户，请重新登录",4000);
        }
        return new ResponseVO<>(userByUserId,"查询成功",4000);
    }


}
